package org.learn.intermediate.smallchange.oop;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/*
* 该类用来完成零钱通主要功能
* */
public class SmallchangeSysOOP {
    boolean loop = true;
    Scanner scanner = new Scanner(System.in);
    String key = "";

    //2、完成零钱通明细
    String details = "------------零钱通明细------------";

    //3、完成收益入账，定义新的变量
    double money = 0;
    double balance = 0;
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm");//用于日期格式化


    //4.消费，新变量
    String note = "";
    //


    public void mainMenu(){
        do{
            System.out.println("\n======选择零钱通菜单======");
            System.out.println("\t\t\t1.零钱通明细");
            System.out.println("\t\t\t2.收益入账");
            System.out.println("\t\t\t3，消费");
            System.out.println("\t\t\t4.退    出");

            System.out.println("请选择(1-4)");
            key = scanner.next();

            //使用switch 分支控制
            switch(key) {
                case "1":
                    this.detail();
                    break;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.pay();
                    break;
                case "4":
                    this.return_();
                    break;
                default:
                    System.out.println("输入有误，请重新选择");

            }
        }while(loop);
        System.out.println("显示零钱通菜单");
    }
    //完成零钱通的明细
    public void detail(){
        System.out.println(details);
    }
    //收益
    public void income(){
        System.out.println("2.收益入账金额：");
        money = scanner.nextDouble();
        //校验
        if(money <= 0){
            System.out.println("收益入账金额需要>0");
            return;//方法里面结束不能用break，用return表示不在执行
        }
        balance += money;
        //拼接当前信息到details
        date = new Date();
        details += "\n收益入账\t" + money + "\t" + sdf.format(date) + "\t" + balance;

    }
    //支出
    public void pay(){
        System.out.println("消费金额:");
        money = scanner.nextDouble();
        if(money <= 0 || money > balance){
            System.out.println("你的消费金额应该在0-" + balance);
           return;
        }
        System.out.println("消费说明：");
        note = scanner.next();


        balance -= money;
        date = new Date();
        details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;

    }
    //退出
public void return_(){
    //使用while break
    //退出while 后判断是否真的退出

    String choice = "";
    while(true) {
        System.out.println("你确定要退出吗？y/n");
        choice = scanner.next();
        if("y".equals(choice) || "n".equals(choice)){
            break;
        }
    }
    //退出while后
    if(choice.equals("y")){
        loop = false;
        System.out.println("4.退     出");
    }

}
}
